package com.wwy.controller;

import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("demo")
public class DemoController {
    @Resource
    private RedisTemplate redisTemplate;

    @GetMapping("set/{value}")
    public Object set(@PathVariable String value){
        // hasKey判断是否存在某个key
        if (redisTemplate.hasKey("test")){
            // getExpire 获取过期时间
            Long ex = redisTemplate.getExpire("test");
            System.out.println("test: "+redisTemplate.opsForValue().get("test"));
            // 低于阈值就增加时间
            if (ex < 80){
                // expire 延时
                redisTemplate.expire("test", 100, TimeUnit.SECONDS);
            }
        }else {
            return "token过期，请重新登录";
        }
        return redisTemplate.getExpire("test");
    }
}
